home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 26
/
Cream of the Crop 26.iso
/
program
/
vol16n12.zip
/
IPC.ZIP
/
PSERVER.ZIP
/
PSVRDLG.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1997-02-13
|
5KB
|
221 lines
// PSvrDlg.cpp : implementation file
//
#include "stdafx.h"
#include <stdlib.h>
#include "PServer.h"
#include "PSvrDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPServerDlg dialog
CPServerDlg::CPServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPServerDlg)
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadStandardIcon(IDI_WINLOGO);
m_hPipe == NULL;
}
void CPServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPServerDlg)
DDX_Control(pDX, IDC_STATUS, m_wndStatus);
DDX_Control(pDX, IDC_PROGRESS, m_wndProgress);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPServerDlg, CDialog)
//{{AFX_MSG_MAP(CPServerDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CREATE()
ON_WM_CLOSE()
//}}AFX_MSG_MAP
ON_MESSAGE (WM_USER_UPDATE, OnUserUpdate)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPServerDlg message handlers
BOOL CPServerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
//
// Initialize the progress control.
//
m_wndProgress.SetRange (0, 10);
m_wndProgress.SetPos (0);
//
// Start a background thread.
//
PIPEINFO* pInfo = new PIPEINFO;
pInfo->hPipe = m_hPipe;
pInfo->hWnd = m_hWnd;
AfxBeginThread (ThreadFunc, pInfo);
return TRUE;
}
void CPServerDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CPServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
int CPServerDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
//
// Create a named pipe.
//
m_hPipe = ::CreateNamedPipe (
"\\\\.\\pipe\\ipcdemo", // Pipe name
PIPE_ACCESS_OUTBOUND, // Write access only
PIPE_TYPE_BYTE | PIPE_NOWAIT, // Write bytes, no waiting
1, // One instance at a time, please
0, // Output buffer size (bytes)
0, // Input buffer size (bytes)
0, // Timeout value (milliseconds)
NULL // Use default security descriptor
);
//
// Check for failures.
//
if (m_hPipe == INVALID_HANDLE_VALUE) {
MessageBox ("Unable to create a named pipe.", "Error",
MB_OK | MB_ICONSTOP);
return -1;
}
if (m_hPipe == NULL) {
MessageBox ("Windows 95 doesn't support named pipe servers. " \
"This application must be run under Windows NT.", "Error",
MB_OK | MB_ICONSTOP);
return -1;
}
//
// Seed the random number generator and return.
//
srand((int) ::GetCurrentTime ());
return 0;
}
void CPServerDlg::OnClose()
{
//
// Close the pipe handle before shutting down.
//
if ((m_hPipe != NULL) && (m_hPipe != INVALID_HANDLE_VALUE)) {
::CloseHandle (m_hPipe);
m_hPipe = NULL;
}
CDialog::OnClose();
}
LONG CPServerDlg::OnUserUpdate (UINT wParam, LONG lParam)
{
//
// Update the display.
//
m_wndProgress.SetPos ((int) wParam);
m_wndStatus.SetWindowText (lParam ? "Connected" : "Not connected");
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// Global functions
UINT ThreadFunc (LPVOID pParam)
{
static BOOL bDisconnect = FALSE;
PIPEINFO* pInfo = (PIPEINFO*) pParam;
HANDLE hPipe = pInfo->hPipe;
HWND hWnd = pInfo->hWnd;
delete pInfo;
BYTE val;
DWORD dwBytesWritten;
while (TRUE) {
val = rand () % 11;
if (IsClientConnected (hPipe)) {
//
// If a client process is connected, write a value to it
// through the pipe and update our own display.
//
::WriteFile (hPipe, &val, 1, &dwBytesWritten, NULL);
::PostMessage (hWnd, WM_USER_UPDATE, (WPARAM) val, 1);
bDisconnect = TRUE;
}
else {
//
// Otherwise, simply update our display. If ::DisconnectNamedPipe
// hasn't been called since the client process disconnected, call
// it now so other clients can attach later.
//
::PostMessage (hWnd, WM_USER_UPDATE, (WPARAM) val, 0);
if (bDisconnect) {
::DisconnectNamedPipe (hPipe); // Let other clients connect
bDisconnect = FALSE;
}
}
::Sleep (1000); // Pause for 1 second
}
return 0;
}
BOOL IsClientConnected (HANDLE hPipe)
{
OVERLAPPED olStruct;
::ZeroMemory (&olStruct, sizeof (OVERLAPPED));
::ConnectNamedPipe (hPipe, &olStruct);
return (::GetLastError () == ERROR_PIPE_CONNECTED) ? TRUE : FALSE;
}